package com.nine.pattern.singleton;

import com.nine.pattern.singleton.demo4.Singleton;

import java.lang.reflect.Constructor;

/**
 * @author nine
 * @version 1.0
 * @description 通过反射，破环单例模式
 * @date 2024-06-26 22:35
 */
public class TestClient02 {

	public static void main(String[] args) {

		// 1. 获取类对象
		Class<Singleton> clazz = Singleton.class;
		try {
			// 2. 获取无参构造
			Constructor<Singleton> cons = clazz.getDeclaredConstructor();
			// 3. 取消访问检查
			cons.setAccessible(true);

			Singleton instance01 = cons.newInstance();
			Singleton instance02 = cons.newInstance();
			System.out.println(instance01 == instance02);
		}
		catch (Exception e) {
			throw new RuntimeException(e);
		}

	}

}
